// 角色定义
export const roles = {
  ADMIN: 'admin',          // 系统管理员
  DATA_ANALYST: 'analyst', // 数据分析师
  CUSTOMER_MANAGER: 'manager', // 客户经理
  MARKETING: 'marketing',  // 营销专员
  DEPARTMENT_HEAD: 'dept_head', // 部门主管
  EXECUTIVE: 'executive'   // 高管用户
}

// 权限定义
export const permissions = {
  // 客户管理权限
  VIEW_CUSTOMERS: 'view_customers',
  EDIT_CUSTOMER: 'edit_customer',
  DELETE_CUSTOMER: 'delete_customer',
  
  // 预测权限
  RUN_PREDICTION: 'run_prediction',
  VIEW_PREDICTIONS: 'view_predictions',
  EXPORT_PREDICTIONS: 'export_predictions',
  
  // 模型管理权限
  VIEW_MODELS: 'view_models',
  TRAIN_MODEL: 'train_model',
  EDIT_MODEL: 'edit_model',
  DELETE_MODEL: 'delete_model',
  
  // 系统设置权限
  SYSTEM_CONFIG: 'system_config',
  USER_MANAGEMENT: 'user_management',
  VIEW_LOGS: 'view_logs',
  
  // 用户管理权限
  VIEW_USERS: 'view_users',
  CREATE_USER: 'create_user',
  EDIT_USER: 'edit_user',
  DELETE_USER: 'delete_user',
  ASSIGN_ROLE: 'assign_role'
}

// 角色权限映射
export const rolePermissions = {
  [roles.ADMIN]: Object.values(permissions), // 管理员拥有所有权限
  
  [roles.DATA_ANALYST]: [
    permissions.VIEW_CUSTOMERS,
    permissions.RUN_PREDICTION,
    permissions.VIEW_PREDICTIONS,
    permissions.EXPORT_PREDICTIONS,
    permissions.VIEW_MODELS,
    permissions.TRAIN_MODEL,
    permissions.EDIT_MODEL,
    permissions.VIEW_LOGS
  ],
  
  [roles.CUSTOMER_MANAGER]: [
    permissions.VIEW_CUSTOMERS,
    permissions.EDIT_CUSTOMER,
    permissions.RUN_PREDICTION,
    permissions.VIEW_PREDICTIONS,
    permissions.VIEW_MODELS
  ],
  
  [roles.MARKETING]: [
    permissions.VIEW_CUSTOMERS,
    permissions.VIEW_PREDICTIONS,
    permissions.EXPORT_PREDICTIONS
  ],
  
  [roles.DEPARTMENT_HEAD]: [
    permissions.VIEW_CUSTOMERS,
    permissions.RUN_PREDICTION,
    permissions.VIEW_PREDICTIONS,
    permissions.VIEW_MODELS,
    permissions.VIEW_LOGS,
    permissions.VIEW_USERS
  ],
  
  [roles.EXECUTIVE]: [
    permissions.VIEW_CUSTOMERS,
    permissions.VIEW_PREDICTIONS,
    permissions.VIEW_MODELS
  ]
}

// 角色数据权限范围
export const roleDataScopes = {
  [roles.ADMIN]: 'all', // 所有数据
  [roles.DATA_ANALYST]: 'all', // 所有数据(但客户数据可能匿名化)
  [roles.CUSTOMER_MANAGER]: 'department', // 部门数据
  [roles.MARKETING]: 'department', // 部门数据
  [roles.DEPARTMENT_HEAD]: 'department', // 部门数据
  [roles.EXECUTIVE]: 'all' // 所有数据(汇总视图)
}

// 角色显示名称
export const roleDisplayNames = {
  [roles.ADMIN]: '系统管理员',
  [roles.DATA_ANALYST]: '数据分析师',
  [roles.CUSTOMER_MANAGER]: '客户经理',
  [roles.MARKETING]: '营销专员',
  [roles.DEPARTMENT_HEAD]: '部门主管',
  [roles.EXECUTIVE]: '高管用户'
}

// 检查用户是否有指定权限
export function hasPermission(userPermissions, requiredPermission) {
  if (!userPermissions || !requiredPermission) {
    return false
  }
  
  // 管理员拥有所有权限
  if (userPermissions.includes('*')) {
    return true
  }
  
  return userPermissions.includes(requiredPermission)
}

// 检查用户是否有指定角色
export function hasRole(userRoles, requiredRole) {
  if (!userRoles || !requiredRole) {
    return false
  }
  
  return userRoles.includes(requiredRole)
}

// 检查用户是否有任意指定角色
export function hasAnyRole(userRoles, requiredRoles) {
  if (!userRoles || !requiredRoles || !Array.isArray(requiredRoles)) {
    return false
  }
  
  return requiredRoles.some(role => userRoles.includes(role))
}

// 检查用户是否有所有指定角色
export function hasAllRoles(userRoles, requiredRoles) {
  if (!userRoles || !requiredRoles || !Array.isArray(requiredRoles)) {
    return false
  }
  
  return requiredRoles.every(role => userRoles.includes(role))
}

// 获取用户可访问的菜单
export function getAccessibleMenus(userRole) {
  const menuPermissions = {
    [roles.ADMIN]: [
      'dashboard', 'datascreen', 'customer', 'prediction', 'model', 'settings'
    ],
    [roles.DATA_ANALYST]: [
      'dashboard', 'datascreen', 'customer', 'prediction', 'model', 'settings'
    ],
    [roles.CUSTOMER_MANAGER]: [
      'dashboard', 'datascreen', 'customer', 'prediction', 'model'
    ],
    [roles.MARKETING]: [
      'dashboard', 'datascreen', 'customer', 'prediction'
    ],
    [roles.DEPARTMENT_HEAD]: [
      'dashboard', 'datascreen', 'customer', 'prediction', 'model', 'settings'
    ],
    [roles.EXECUTIVE]: [
      'dashboard', 'datascreen', 'customer', 'prediction', 'model'
    ]
  }
  
  return menuPermissions[userRole] || []
} 